<template>
  <div>
    <el-form :model="form" ref="deptForm" label-width="120px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, updateDepartments } from '@/api/departments'

export default {
  name: '',
  props: {
    pid: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean
    },
    isEdit: {
      type: Boolean
    },
    row: {
      // type:[Object,Array]
      type: Object
    }
  },
  data() {
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      },
      managerList: [],
      rules: {
        name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入部门编码', trigger: 'blur' }],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {
    isEdit: {
      handler(newVal, oldVal) {
        console.log(newVal, this.row, 88855)
        //  根据isEdit=true表示式编辑进入的  =====将row赋值给form
        if (newVal) {
          this.form = this.row
          // 因为数据是传递过来的.直接使用表单双向绑定会影响源头数据 所以需要使用深拷贝==lodash
          this.form = JSON.parse(JSON.stringify(this.row))
        }
      },
      immediate: true // 初次进入立即执行
    }
  },
  created() {
    this.getEmp()
  },
  methods: {
    async getEmp() {
      const { data } = await getEmployeeSimple()
      console.log(data, 22255)
      this.managerList = data
    },
    submit() {
      // 点击对话框中的确认按钮
      this.$refs.deptForm.validate((valid) => {
        // console.log(valid, 888)
        if (valid) {
          this.doAdd()
        }
      })
    },
    async doAdd() {
      //  先将pid赋值给form中的pid
      this.form.pid = this.pid
      // console.log(this.form, 888)
      // 实现添加部门的接口函数调用
      const res = await addDepartments(this.form)
      // console.log(res, 886)
      if (res.code === 10000) {
        // 表示添加部门成功
        this.$emit('success')
        // 清空数据
        this.form = {
          name: '',
          code: '',
          manager: '',
          introduce: '',
          pid: ''
        }
        this.close()
      }
    },
    async doEdit() {
      // 编辑确认按钮
      // 1.封装api接口函数
      // 2.调用api函数
      // 3.成功后:关闭对话框,清空数据,刷新页面
      const res = await updateDepartments(this.form)
      if (res.code === 10000) {
        this.$emit('success') // 关闭对话框
      }
    },
    cancel() {
      this.$emit('update:visible', false)
    },
    close() {
      // 清空数据
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      }
      // 表单重置
      this.$refs['deptForm'].resetFields()
    }
  }
}
</script>
<style lang="less" scoped></style>